from selenium.webdriver.common.by import By

from web.framework.base_page import BasePage


class SearchPage(BasePage):
    def __init__(self, driver=None):
        super().__init__(driver)

    def search_keyword(self, keyword):
        """搜索关键字"""
        input_btn = (By.CSS_SELECTOR, '[placeholder="搜索"]')
        self.find(input_btn).send_keys(keyword)
        return self

    def search_category(self, keyword):
        """搜索分类"""
        category_btn = (By.ID, 'search-in-category-header')
        input_btn = (By.CLASS_NAME, 'filter-input')
        result_btn = (By.CLASS_NAME, 'select-kit-row')
        no_content = (By.CSS_SELECTOR, '.no-content')
        self.click(category_btn)
        self.find(input_btn).send_keys(keyword)
        # self.find(no_content)
        self.click(result_btn)
        return self

    def search_tags(self, keyword):
        """搜索标签"""
        tag_btn = (By.ID, 'search-with-tags-header')
        input_btn = (By.CSS_SELECTOR, '[placeholder="可选标签"]')
        result_first = (By.CSS_SELECTOR, '.tag-chooser-row')
        self.click(tag_btn)
        self.find(input_btn).send_keys(keyword)
        self.click(result_first)
        return self

    def search_topic_status(self, status):
        """搜索话题状态"""
        topic_btn = (By.ID, 'search-status-options-header')
        status_btn = (By.CSS_SELECTOR, f'[title={status}]')
        self.click(topic_btn)
        self.click(status_btn)
        return self

    def search_click(self):
        """点击搜索按钮"""
        search_btn = (By.CLASS_NAME, 'search-cta')
        result = (By.CSS_SELECTOR, '.topic-title')
        self.click(search_btn)
        title_list = []
        result_list = self.finds(result)
        if len(result_list) > 0:
            for r in result_list:
                title_list.append(r.text)
            return title_list
        else:
            return None
